package com.kt.mes.service.impl;

import java.util.List;

import com.kt.mes.domain.ProcessRouteItemParam;
import com.kt.mes.domain.ToMes;
import com.kt.mes.domain.WorkPlanItem;
import com.kt.mes.mapper.ProcessRouteItemParamMapper;
import com.kt.mes.mapper.WorkPlanItemMapper;
import com.kt.mes.service.IStockBinListService;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.kt.mes.mapper.StockBinMapper;
import com.kt.mes.domain.StockBin;
import com.kt.mes.service.IStockBinService;

/**
 * 料仓Service业务层处理
 * 
 * @author lzc
 * @date 2021-01-26
 */
@Service
public class StockBinServiceImpl implements IStockBinService 
{
    @Autowired
    private StockBinMapper stockBinMapper;
    @Autowired
    private ProcessRouteItemParamMapper processRouteItemParamMapper;
    @Autowired
    private WorkPlanItemMapper workPlanItemMapper;

    @Autowired
    private IStockBinListService stockBinListService;
    /**
     * 查询料仓
     * 
     * @param id 料仓ID
     * @return 料仓
     */
    @Override
    public StockBin selectStockBinById(Long id)
    {
        return stockBinMapper.selectStockBinById(id);
    }

    @Override
    public StockBin selectStockBinBySbId(String sbId) {
         return stockBinMapper.selectStockBinBySbId(sbId);
    }

    /**
     * 查询料仓列表
     * 
     * @param stockBin 料仓
     * @return 料仓
     */
    @Override
    public List<StockBin> selectStockBinList(StockBin stockBin)
    {
        return stockBinMapper.selectStockBinList(stockBin);
    }

    /**
     * 新增料仓
     * 
     * @param stockBin 料仓
     * @return 结果
     */
    @Override
    public int insertStockBin(StockBin stockBin)
    {
        stockBin.setCreateTime(DateUtils.getNowDate());
        return stockBinMapper.insertStockBin(stockBin);
    }

    /**
     * 修改料仓
     * 
     * @param stockBin 料仓
     * @return 结果
     */
    @Override
    public int updateStockBin(StockBin stockBin)
    {
        stockBin.setUpdateTime(DateUtils.getNowDate());
        return stockBinMapper.updateStockBin(stockBin);
    }

    /**
     * 批量删除料仓
     * 
     * @param ids 需要删除的料仓ID
     * @return 结果
     */
    @Override
    public int deleteStockBinByIds(Long[] ids)
    {
        return stockBinMapper.deleteStockBinByIds(ids);
    }

    /**
     * 删除料仓信息
     * 
     * @param id 料仓ID
     * @return 结果
     */
    @Override
    public int deleteStockBinById(Long id)
    {
        return stockBinMapper.deleteStockBinById(id);
    }

    @Override
    public void outStockBinByWpi(ToMes toMes){

        //先找wpi

        WorkPlanItem wpi_query=new WorkPlanItem();
        wpi_query.setOpId(toMes.getoId());
        wpi_query.setOpsNum(Integer.valueOf(toMes.getOposId()));
        wpi_query.setStepNum(Integer.valueOf(toMes.getStepNum()));
        List<WorkPlanItem> list_wpis= workPlanItemMapper.selectWorkPlanItemList(wpi_query);
        if(!list_wpis.isEmpty() && list_wpis.size()==1){

            WorkPlanItem wpi=list_wpis.get(0);

            //找参数
            ProcessRouteItemParam processRouteItemParam = new ProcessRouteItemParam();
            processRouteItemParam.setTpriId(wpi.getTpriId());
            List<ProcessRouteItemParam> list_param = processRouteItemParamMapper.selectProcessRouteItemParamList(processRouteItemParam);
            if (list_param.isEmpty() == false) {
                ProcessRouteItemParam param01= list_param.get(0);

                if(param01.getParamValue().equals("a")){
                    //出a 仓物料

                    stockBinListService.outStockBinByTsbId(selectStockBinBySbId("A").getId());

                }
                if(param01.getParamValue().equals("b")){
                    stockBinListService.outStockBinByTsbId(selectStockBinBySbId("B").getId());

                }



            }
        }



    }
}
